/**
 * 权限相关的数据
 * Created by gcc on 2017/9/29.
 */
import Vue from "vue";

/**
 * 包裹售后服务操作列
 * @param self
 * @returns {{title: string, align: string, width: number, render: render}}
 */
const rowPackageAftersaleOp = function (self) {
  return !self.$authority('ORDER-2-100')?null: {
    title: '操作',
    fixed: self.$yMobile()?'none':'right',
    align: 'center',
    width: 110,
    render: (h, params) => {
      let prop, btnContent;
      if(params.row.processTime) {
        prop = {size: 'small', icon: "ios-information-circle-outline", type: 'info'};
        btnContent = "查看";
      }else {
        prop = {size: 'small', icon: "ios-hand", type: 'success'};
        btnContent = "处理";
      }
      return h('Button', {
        props: prop,
        on: {
          click: () => {
            self.getPackageAftersale(params.row.id, params.row.orderId);
          }
        }
      }, btnContent);
    }
  }
};

//未收到货售后处理校验
const validateUnReceivedRefundAmount = (rule, value, callback) => {
  let valRegex = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
  if (params.unReceived.result == 101 && (!valRegex.test(value) || value < 0)) {
      callback(new Error('请输入正确的退款金额，最多两位小数！'));
  } else {
    callback();
  }
};
const validateDeliveryCompany = (rule, value, callback) => {
  if (params.unReceived.result == 301 && !value) {
    callback(new Error('请选择快递公司！'));
  }else {
    callback();
  }
};
const validateWaybill = (rule, value, callback) => {
  if (params.unReceived.result == 301 && !value) {
    callback(new Error('请输入运单号！'));
  }else {
    callback();
  }
};
const validateAftersaleItemRefundAmount = (rule, value, callback) => {
  let valRegex = /^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/;
  if (params.aftersaleItem.result == 101 && (!valRegex.test(value) || value < 0)) {
    callback(new Error('请输入正确的退款金额，最多两位小数！'));
  } else {
    callback();
  }
};

const rowProductImg = function(self){
  return {
    title: '产品图片',
    key: 'productImg',
    align: 'center',
    width: 120,
    render:(h, params) => {
      let orderItem = params.row.orderItem;
      let imgSrc = self.yunPath + (orderItem.zipImages != null ?orderItem.zipImages[0].imagePath : orderItem.originalImagePath);
      return h('img', {
        attrs: {src: imgSrc},
        style: {height:'80px',width:'100px'}
      });
    }
  }
};

const rowAftersaleType = function(self){
  return {
    title: '售后类型',
    key: 'type',
    align: 'center',
    width: 120,
    render:(h, params) => {
      let type ='';
      if(params.row.type){
        type = self.aftersaleTypeDict.filter(itemDict => itemDict.value == params.row.type)[0].name;
      }
      return h('div', type);
    }
  };
};

const rowAftersaleProcessType = function(self){
  return {
    title: '处理结果',
    key: 'result',
    align: 'center',
    width: 180,
    render:(h, params) => {
      let type ='';
      if(params.row.result){
        type = self.aftersaleProcessTypeDict.filter(itemDict => itemDict.value == params.row.result)[0].name;
      }
      return h('div', type);
    }
  };
}

/**
 * 订单项售后服务操作列
 * @param self
 * @returns {{title: string, align: string, width: number, render: render}}
 */
const rowAftersaleItemOp = function (self) {
  return {
    title: '操作',
    align: 'center',
    width:110,
    render: (h, params) => {
      if(!self.$authority('ORDER-2-102')){
        return h('div', "");
      }
      let prop, btnContent;
      if(params.row.processTime) {
        prop = {size: 'small', icon: "ios-information-circle-outline", type: 'info'};
        btnContent = "查看";
      }else {
        prop = {size: 'small', icon: "ios-hand", type: 'success'};
        btnContent = "处理";
      }
      return h('Button', {
        props: prop,
        on: {
          click: () => {
            self.toAftersaleItem(params.row);
          }
        }
      }, btnContent);
    }
  }
};

// 页码相关
let page = {
  total: 1,
  startIndex: 0,
  stepNumber: 40,
  currentPage: 1
};

//页面状态相关
let status = {
  //显示包裹售后详情的 modal
  modalDetail: {
    isShow: false,
  },
  //查看图片
  modalImage: {
    isShow: false,
    url: ''
  },
  //未收到货包裹售后处理
  modalUnReceived: {
    isShow: false,
    isLoading:true
  },
  modalAftersaleItem :{
    isShow :false,
    isLoading: true,
    submitLoading : false
  },
  rules: {
    ruleUnReceived: {
      result:[
        { required: true, type: 'integer', message: '请选择处理方式！', trigger: 'change' },
      ],
      refundAmount: [
        { validator: validateUnReceivedRefundAmount, trigger: 'blur' }
      ],
      deliveryCompany: [
        { validator: validateDeliveryCompany, trigger: 'blur' }
      ],
      waybill: [
        { validator: validateWaybill, trigger: 'blur' }
      ]
    },
    ruleAftersaleItem: {
      result:[
        { required: true, type: 'integer', message: '请选择处理方式！', trigger: 'change' },
      ],
      refundAmount: [
        { validator: validateAftersaleItemRefundAmount, trigger: 'blur' }
      ]
    }
  },
  exportDialog:{
  isShowDetail:false,
  orderId:''
  }
};

// 和网络交互相关的参数
let params = {
  //搜索条件
  searchParams:{
    orderSerialNumber: '',
    isProcess:''
  },
  unReceived:{
    id : "",
    packageId : "",
    result : "",
    opinion : "",
    waybill : "",
    deliveryCompany : "",
    refundAmount : ""
  },
  aftersaleItem:{
    id : "",
    orderId : "",
    serialNumber : "",
    packageAftersaleId:"",
    orderItemId : "",
    number : "",
    type : "",
    result : "",
    opinion : "",
    refundAmount : "",
    tempRefundAmount : "",//保存默认的退款金额，不需要提交到后台
    cause : "",
    certificate : [],
    orderItem :{},
    processTime:""
  }
};

// 页面的数据源
let data = {
  packageAftersaleList: [],
  packageAftersale:{
    id:"",
    orderId:"",
    serialNumber:"",
    packageId:"",
    waybill:"",
    userName:"",
    type:"",
    typeTitle:"",
    handlerName:"",
    result:"",
    resultTitle:"",
    opinion:"",
    refundAmount:"",
    processTime:"",
    createtime:"",
    itemAftersales:[]
  }
};

let table = {
  tableMain: {
    rowIndex: {
      type: 'index',
      title: '#',
      key: 'index',
      fixed: 'left',
      align: 'center',
      width: 65
    },
    rowSerialNumber: function(self){
      return {
        title: '订单流水号',
        key: 'serialNumber',
        align: 'center',
        minWidth: 220,
        render: (h, params) => {
          return !self.$authority("ORDER-1-205")?h('div',params.row.serialNumber):
          h('div', [h('a', {
            style: {fontSize: "0.8rem"},
            on: {
              click: () => {
                self.showOrderDetail(params.row.orderId);
              }
            }
          }, params.row.serialNumber),
          ]);
        }
      }

    },
    rowWaybill: {
      title: '运单号',
      key: 'waybill',
      align: 'center',
      minWidth: 180
    },
    rowAftersaleType:{
      width: 110,
      title: '售后类型',
      key: 'type',
      render:(h, params) => {
        let type ='';
        if(params.row.type == 101){
          type = '已收到货物';
        }else if(params.row.type == 100){
          type = '未收到货物';
        }
        return h('div', type);
      }
    },
    rowUser: {
      width: 150,
      title: '用户编号',
      key: 'encodeName',
      // render: (h, params) => {
        // let username = Vue.prototype.$tiyHideUsername(params.row.userName);
        // return (
        //   <div>
        //     <Tooltip content={params.row.userName} placement="top">
        //       <span style="padding:6px;font-size:13px" >
        //         {username}
        //       </span>
        //     </Tooltip>
        //   </div>
        // );

      // }
    },
    rowCreateTime: {
      title: '提交时间',
      key: 'createtime',
      align: 'center',
      width: 250,
      render: (h, params) => {
        return h('div', Vue.prototype.$tiyBJTime(params.row.createtime));
      }
    },
    rowProcessTime: {
      title: '处理时间',
      key: 'processTime',
      align: 'center',
      width: 250,
      render: (h, params) => {
        if(params.row.processTime){
          return h('div', Vue.prototype.$tiyBJTime(params.row.processTime));
        } else {
          return h('div','');
        }
      }
    },
    rowOpertion:rowPackageAftersaleOp
  },
  tableAftersaleItem :{
    rowIndex: {
      type: 'index',
      title: '#',
      key: 'index',
      fixed: 'left',
      align: 'center',
      width: 60
    },
    rowCode: {
      title: '产品编码',
      key: 'productCode',
      align: 'center',
      width: 140,
      render: (h, params) => {
        let productUrl = Vue.prototype.$tiyProductUrl(params.row.orderItem.productCode);
        return h('div',[h('a',{
          attrs:{href:productUrl,target:'_blank'}
        },params.row.orderItem.productCode)]);
      }
    },
    rowPic: rowProductImg,
    rowColorName: {
      title: '色卡',
      key: 'color',
      align: 'center',
      width: 120,
      render:(h, params) => {
        return h('div', params.row.orderItem.color);
      }
    },
    rowSizeName: {
      title: '尺码',
      key: 'sizeName',
      align: 'center',
      width: 120,
      render:(h, params) => {
        return h('div', params.row.orderItem.sizeName);
      }
    },
    rowType: rowAftersaleType,
    rowResult: rowAftersaleProcessType,
    rowProcesser: {
      title: '处理人',
      key: 'handlerName',
      align: 'center',
      width: 120,
    },
    rowOpertion:rowAftersaleItemOp
  }
};


function prepareMainTables(self) {

  let titles = [
    table.tableMain.rowIndex,
    table.tableMain.rowSerialNumber(self),
    table.tableMain.rowWaybill,
    table.tableMain.rowAftersaleType,
    table.tableMain.rowUser,
    table.tableMain.rowCreateTime,
    table.tableMain.rowProcessTime,
    table.tableMain.rowOpertion(self)
  ];

  return self.$authTableTitles(titles)
}




export default  {
  tPage: page,
  tStatus: status,
  tParams: params,
  tTable: table,
  tableTitles:prepareMainTables,
  tData: data
};
